package com.iteasy.manage.aop;

import org.iteasy.framework.commons.UserSession;
import org.iteasy.framework.utils.BeanUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Objects;
import java.util.Optional;

/**
 * Created by bo.chen on 2020/04/27 10:37:58
 */
@Aspect
@Component
public class DaoAop {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private static String CREATE_USER = "createUser";
    private static String CREATE_TIME = "createTime";
    private static String UPDATE_USER = "updateUser";
    private static String UPDATE_TIME = "updateTime";

    @Pointcut("execution(public * com.iteasy.manage.*.dao.*.*save*(..))")
    private void saveBefore() {}

    @SuppressWarnings("rawtypes")
    @Before(value = "saveBefore()")
    public void saveBefore(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
    }

    /**
     * 填充数据
     * @param obj
     */
    private void fill(Object obj) {
//        UserDto userDto = UserSession.get();
//        if (Objects.isNull(userDto)) {
//            return;
//        }
//        Long userId = Optional.ofNullable(userDto).orElseGet(() -> new UserDto()).getId();
//        if (Objects.isNull(userId)) {
//            return;
//        }
//        BeanUtil.invokSet(obj, UPDATE_USER, userId);
//        BeanUtil.invokSet(obj, CREATE_USER, userId);
//        BeanUtil.invokSet(obj, CREATE_TIME, new Date());
//        BeanUtil.invokSet(obj, UPDATE_TIME, new Date());
    }
}
